<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <!-- table -->
      <FormPanel ref="formPanel" :list="formList" :cols="1" :formType="'add'" labelWidth="100" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <div style="padding-top: 0.5rem;"></div>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    </Spin>
  </div>
</template>
<script>
import { editSave ,getTjdetail } from '@/api/claim/factory/GenerallyCarPartTypeWarrantyMaintain';
import Vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import { getModelsdictt } from '@/api/dictionary/index';
import {dictionary} from'@/utils/dictMixin';

export default {
  mixins: [dictionary],
  props: ['type', 'rowData'],
  components: {
    Vehicle
  },
  data() {
    return {
      visible: false,
      visibleDrawer: false,
      visibleAdd: false,
      flterValue: {}, //参数
      fetchParams: {}, //ipa 接口
      formList: this.createFormList(),
      saveData: null,
      val: null,
      createdBy:'',
      loading: false,
      modelNameList: []
    };
  },
  async mounted() {
    this.chuqi();
    await this.getDetailFun(this.rowData.ptId);
  },
  methods: {
    async chuqi(){
      const res = await getModelsdictt();
      if (res.resultCode == 200) {
        res.data.forEach(x => {
              if(x != null){
                this.modelNameList.push({text: x.MODEL_NAME,value: x.MODEL_CODE,id: x.MODEL_ID});
              }
            });
        this.formList.find(x => x.fieldName == 'seriesCode').itemList = this.modelNameList;
      }
    },
    async getDetailFun(id) {
      this.loading = true;
      let res = (await getTjdetail({ ptId: id })).data;
      for (let i in res) {
        this.formList.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res[i];
          }
        });
      }
      this.ptStatus = res.ptStatus;
      this.ownerCode = res.ownerCode;
      this.appId = res.appId;
      this.ownerParCode = res.ownerParCode;
      this.isDeleted = res.isDeleted;
      this.createdBy = res.createdBy;
      this.pModelCode = res.pModelCode;
      this.createdAt = res.createdAt;
      this.powerType = res.powerType;
      this.loading = false;
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        for(let key in this.saveData){
          this.rowData[key] = this.saveData[key];
        }
        this.rowData.createdBy = this.createdBy;
        this.rowData.createdAt = this.createdAt;
        this.rowData.isDeleted = this.isDeleted;
        this.rowData.appId = this.appId;
        this.rowData.pModelName = this.pModelName;
        this.rowData.updatedAt = null;
        this.rowData.updatedBy = null;
        this.modelNameList.forEach(x => {
        if(x.value === this.formList.find(x => x.fieldName == 'seriesCode').initialValue){
            this.rowData.seriesId = x.id;
            this.rowData.seriesCode = x.value;
            this.rowData.seriesName = x.text;
          }
        });
        this.loading = true;
        let res = await editSave(this.rowData);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({title: '提示信息', message: '保存成功'});
        } else {
          this.loading = false;
        }
      }
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
    },
    //取消操作
    closeDialog(type) {
      if(type == 'refresh') {
        this.$emit('save', { visible: false, data: this.selectedRows, type: 'close' });
      } else {
        this.$emit('close', { visible: false, data: this.selectedRows, type: 'close' });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '备件类型',
          fieldName: 'typeId',
          placeholder: '请选择配件类型...',
          initialValue: '',
          filterable: true,
          itemList: this.createDictList('5509'),
          rules: [{required: true, message: '请选择配件类型...', trigger: 'change'}],
          disabled: true
        },
        {
          type: 'SELECT',
          label: '适用车型',
          fieldName: 'seriesCode',
          placeholder: '请选择',
          itemList: [],
          rules: [{required: true, message: '请选择适用车型...', trigger: 'change'}],
          disabled: true
        },
        {
          type: 'SELECT',
          label: '营运类型',
          placeholder: '请选择营运类型...',
          fieldName: 'ptType',
          itemList: [
            { text: '运营', value: 55841001 },
            { text: '非运营', value: 55841002 },
          ],
          filterable: true,
          rules: [{required: true, message: '请选择营运类型...', trigger: 'change'}],
        },
        {
          type: 'INPUT',
          label: '保修期',
          fieldName: 'pRpairM',
          initialValue: '',
          placeholder: '请输入保修期...',
          rules: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入保修期...'));
                }
                if (!Number.isInteger(Number(value))) {
                  return callback(new Error('请输入数字值...'));
                }
                if(Number(value)>999) {
                  return callback(new Error('请输入三位数的保修期...'));
                }
                callback();
              },
              trigger: ['blur', 'change'],
            },
          ],
          unitRender: () => {
            return <span>(月)</span>;
          },
        },
        {
          type: 'INPUT',
          label: '保修里程',
          fieldName: 'pMile',
          initialValue: '',
          placeholder: '请输入保修里程...',
          rules: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入保修里程...'));
                }
                if (!Number.isInteger(Number(value))) {
                  return callback(new Error('请输入数字值...'));
                }
                if(Number(value)>999999) {
                  return callback(new Error('请输入六位数的保修里程...'));
                }
                callback();
              },
              trigger: ['blur', 'change'],
            },
          ],
          unitRender: () => {
            return <span>(KM)</span>;
          },
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'pStatus',
          placeholder: '请选择状态',
          filterable: true,
          itemList: [{text: '启用', value: 1}, {text: '禁用', value: 0}],
          rules: [{required: true, message: '请选择状态...', trigger: 'change'}],
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
